//
//  CoreSpotlightTestController.m
//  TestDemo
//
//  Created by fengxijun on 2/27/16.
//  Copyright © 2016 goappbox. All rights reserved.
//

#import "CoreSpotlightTestController.h"
#import <CoreSpotlight/CoreSpotlight.h>
#import "CoreSpotlightImageController.h"
#import "CoreSpotlightTxtController.h"
#import "CoreSpotlightWebController.h"

@interface CoreSpotlightTestController ()
@property (nonatomic, strong) NSArray *tableData;
@end

@implementation CoreSpotlightTestController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.tableData = @[@"Image",@"TXT",@"Web"];
    [self saveData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = self.tableData[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            CoreSpotlightImageController *image = [[CoreSpotlightImageController alloc] init];
            [self.navigationController pushViewController:image animated:YES];
        }
            break;
        case 1:
        {
            CoreSpotlightTxtController *txt = [[CoreSpotlightTxtController alloc] init];
            [self.navigationController pushViewController:txt animated:YES];
        }
            break;
        case 2:
        {
            CoreSpotlightWebController *web = [[CoreSpotlightWebController alloc] init];
            [self.navigationController pushViewController:web animated:YES];
        }
            break;
        default:
            break;
    }
}

-(void)showViewControllerWithIdentifier:(NSString *)identifier {
    if ([identifier isEqualToString:self.tableData[0]]) {
        CoreSpotlightImageController *image = [[CoreSpotlightImageController alloc] init];
        [self.navigationController pushViewController:image animated:YES];
    } else if ([identifier isEqualToString:self.tableData[1]]) {
        CoreSpotlightTxtController *txt = [[CoreSpotlightTxtController alloc] init];
        [self.navigationController pushViewController:txt animated:YES];
    } else if ([identifier isEqualToString:self.tableData[2]]) {
        CoreSpotlightWebController *web = [[CoreSpotlightWebController alloc] init];
        [self.navigationController pushViewController:web animated:YES];
    }
}

- (void)saveData
{
    NSMutableArray *seachableItems = [NSMutableArray new];
    [self.tableData enumerateObjectsUsingBlock:^(NSString *__nonnull obj, NSUInteger idx, BOOL * __nonnull stop) {
        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"views"];
        attributeSet.title = obj;
        attributeSet.contentDescription = [NSString stringWithFormat:NSLocalizedString(@"a easy way to open %@", nil),obj];
        UIImage *thumbImage = [UIImage imageNamed:[NSString stringWithFormat:@"icon_%@.png",obj]];
        attributeSet.thumbnailData = UIImagePNGRepresentation(thumbImage);//beta 1 there is a bug
        CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:obj                                                                                                                                    domainIdentifier:@"com.kdanmobile.CoreSpotlightDemo"                                                                                                        attributeSet:attributeSet];
        [seachableItems addObject:item];
    }];
    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:seachableItems
                                                   completionHandler:^(NSError * __nullable error) {                                                              if (!error)
                                                       NSLog(@"%@",error.localizedDescription);
                                                   }];
}

@end
